package org.example;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * ClassName: WuDaInvocationHandler
 * Package: org.example
 * Description:
 *
 * @Author wanghao
 * @Create 2024-07-08 8:56
 * @Version 1.0
 */
public class WuDaInvocationHandler implements InvocationHandler {

    private ISale target;


    public WuDaInvocationHandler(ISale target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String methodName = method.getName();
        // 赠送的规则 - 切入点
        if (methodName.endsWith("Bing")) {
            song();
        }
        return method.invoke(target, args);
    }

    private void song() {
        System.out.println("我是五道口代理，我给你唱一首歌");
    }

    public Object createProxy() {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }
}
